﻿using IOP.SgrA;
using IOP.SgrA.ECS;
using IOP.Extension.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Numerics;
using System.Runtime.InteropServices;

namespace VkSample911
{
    public class LightSystem : ComponentSystem
    {
        [Autowired]
        private IGraphicsManager GraphicsManager;
        private Ambient Ambient = new Ambient(0.15f, 0.15f, 0.15f, 0.15f);
        private Camera Camera;

        public override void Initialization()
        {
            GraphicsManager.TryGetCamera("main", out Camera);
        }

        public override void Update(TimeSpan lastStamp)
        {
            ContextManager.Foreach<LightGroup>((group) => 
            {
                Context context = ContextManager.GetContext(group.Entity);
                var obj = context.GetContextRenderObject();
                var render = context.GetContextRenderGroup();
                PointLight point = obj.GetComponents<PointLight>().First();
                Span<Vector4> data = stackalloc Vector4[]
                {
                    new Vector4(Camera.EyePosition, 1),
                    point.Position,
                    point.Strength
                };
                Span<byte> p = MemoryMarshal.AsBytes(data);
                render.PushDataToTexture(p[..40], 0, 0);
            });
        }
    }

    public struct LightGroup
    {
        public Entity Entity;
        public LightComponent Light;
    }
}
